package com.easy.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/*")
public class ParamsFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		Map params = new HashMap();
		Map<String, String[]> re_params = request.getParameterMap();
		Set<String> keys = re_params.keySet();
		String[] temp;
		for (String key : keys) {
			temp = re_params.get(key);
			if (temp.length == 1) {
				params.put(key, temp[0]);
			} else {
				params.put(key, temp);
			}
		}
		request.setAttribute("params", params);
		chain.doFilter(request, response);
	}

	public ParamsFilter() {
	}

	public void destroy() {
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
